/*
 * $Id$
 */
package org.crazydays.gameplan.widget;


import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Set;

import org.crazydays.gameplan.db.io.XMLDecoderInputStreamWrapper;
import org.crazydays.gameplan.db.io.XMLEncoderOutputStreamWrapper;

/**
 * <h1>WidtgetIO</h1>
 * <p>
 * Widget IO related methods.
 * </p>
 */
public class WidgetIO
{
    /**
     * Store categories.
     * 
     * @param categories Categories
     * @param file File
     * @throws IOException
     */
    public final static void storeCategories(Set<WidgetCategory> categories,
        File file)
        throws IOException
    {
        if (file == null) {
            throw new IllegalArgumentException("file == null");
        }

        OutputStream outputStream = new FileOutputStream(file);
        try {
            storeCategories(categories, outputStream);
        } finally {
            outputStream.close();
        }
    }

    /**
     * Store categories.
     * 
     * @param categories Categories
     * @param outputStream OutputStream
     * @throws IOException
     */
    public final static void storeCategories(Set<WidgetCategory> categories,
        OutputStream outputStream)
        throws IOException
    {
        if (categories == null) {
            throw new IllegalArgumentException("categories == null");
        }

        XMLEncoder geometryEncoder =
            new XMLEncoder(new XMLEncoderOutputStreamWrapper(outputStream));
        geometryEncoder.writeObject(categories);
        geometryEncoder.close();
    }

    /**
     * Load widget categories file.
     * 
     * @param file File
     * @return WidgetCategories
     * @throws IOException
     */
    public final static Set<WidgetCategory> loadCategories(File file)
        throws IOException
    {
        if (file == null) {
            throw new IllegalArgumentException("file == null");
        }

        InputStream inputStream = new FileInputStream(file);
        try {
            return loadCategories(inputStream);
        } finally {
            inputStream.close();
        }
    }

    /**
     * Load widget categories from input stream.
     * 
     * @param inputStream InputStream
     * @return WidgetCategories
     * @throws IOException
     */
    public final static Set<WidgetCategory> loadCategories(
        InputStream inputStream)
        throws IOException
    {
        if (inputStream == null) {
            throw new IllegalArgumentException("inputStream == null");
        }

        inputStream = new XMLDecoderInputStreamWrapper(inputStream);

        XMLDecoder decoder = new XMLDecoder(inputStream);
        try {
            @SuppressWarnings("unchecked")
            Set<WidgetCategory> categories =
                (Set<WidgetCategory>) decoder.readObject();
            return categories;
        } finally {
            decoder.close();
        }
    }
}
